package com.yaodian100.ec.sms.web;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.yaodian100.core.admin.service.UserService;
import com.yaodian100.core.common.entity.SimplePager;
import com.yaodian100.core.common.service.DomainService;
import com.yaodian100.core.menu.entity.Menu;
import com.yaodian100.core.menu.entity.Option;
import com.yaodian100.core.sms.entity.Sms;
import com.yaodian100.core.sms.service.SmsService;

public class SmsAction extends ActionSupport implements Preparable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2846751639186279674L;

	@Autowired
	private SmsService smsService;
	@Autowired
	private UserService userService;
	private SimplePager pager;
	@Autowired
	private DomainService<Menu> menuService;
	private Menu contactMenu;
	private String startDate;
	private String endDate;
	private Sms sms;
	private List<Sms> smsList;
	private Map<String, Option> contacts = new HashMap<String, Option>();
	private List<Option> contactsKey;
	private String[] sendList;
	private String smsMsg;
	private String smsId;

	private String orderId;
	private String orderContact;

	public DomainService<Menu> getMenuService() {
		return menuService;
	}

	public void setMenuService(DomainService<Menu> menuService) {
		this.menuService = menuService;
	}

	public Menu getContactMenu() {
		return contactMenu;
	}

	public void setContactMenu(Menu contactMenu) {
		this.contactMenu = contactMenu;
	}

	public String getStartDate() {
		return startDate;
	}

	public void setStartDate(String startDate) {
		this.startDate = startDate;
	}

	public String getEndDate() {
		return endDate;
	}

	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}

	public Sms getSms() {
		return sms;
	}

	public void setSms(Sms sms) {
		this.sms = sms;
	}

	public List<Sms> getSmsList() {
		return smsList;
	}

	public void setSmsList(List<Sms> smsList) {
		this.smsList = smsList;
	}

	public Map<String, Option> getContacts() {
		return contacts;
	}

	public void setContacts(Map<String, Option> contacts) {
		this.contacts = contacts;
	}

	public List<Option> getContactsKey() {
		return contactsKey;
	}

	public void setContactsKey(List<Option> contactsKey) {
		this.contactsKey = contactsKey;
	}

	public String[] getSendList() {
		return sendList;
	}

	public void setSendList(String[] sendList) {
		this.sendList = sendList;
	}

	public String getSmsMsg() {
		return smsMsg;
	}

	public void setSmsMsg(String smsMsg) {
		this.smsMsg = smsMsg;
	}

	public String getSmsId() {
		return smsId;
	}

	public void setSmsId(String smsId) {
		this.smsId = smsId;
	}

	public String getOrderId() {
		return orderId;
	}

	public void setOrderId(String orderId) {
		this.orderId = orderId;
	}

	public String getOrderContact() {
		return orderContact;
	}

	public void setOrderContact(String orderContact) {
		this.orderContact = orderContact;
	}

	public String createUrgentSms() {

		contacts = contactMenu.getOptions();
		contactsKey = new ArrayList();
		for (Option arrayTemplate : contacts.values()) {
			contactsKey.add(arrayTemplate);
		}

		return INPUT;
	}

	public String sendUrgentSms() {
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd-HHmmss");
		String setId = df.format(new Date());
		if (sendList.length > 0) {
			for (int i = 0; i < sendList.length; i++) {
				String caller = sendList[i];
				smsService.sendSms(caller, smsMsg, "SOS-" + setId);
			}

		}
		smsList = smsService.querySmsByOrderId("SOS-" + setId);

		return "view";
	}

	public String viewUrgentSms() {
		if (StringUtils.isNotBlank(smsId)) {
			smsList = smsService.querySmsByOrderId(smsId);
		}
		return "view";
	}

	public String queryUrgentSms() {
		SimpleDateFormat dfOld = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat dfNew = new SimpleDateFormat("yyyyMMdd");
		if (StringUtils.isNotBlank(startDate)) {

			String sDate;
			try {
				sDate = dfNew.format(dfOld.parse(startDate));
			} catch (ParseException e) {
				sDate = StringUtils.replace(startDate, "-", "");
			}
			List<Sms> tempSmsList = smsService.queryEmergencySmsByDate(sDate);
			smsList = new ArrayList<Sms>();
			String lastOrderId = "";
			for (Sms sms : tempSmsList) {
				if (!lastOrderId.equals(sms.getOrderId())) {
					sms.setCreateUser(userService.get(sms.getCreateUser()).getNameNative());
					smsList.add(sms);
				}
				lastOrderId = sms.getOrderId();
			}
			// pager.setTotalSize(smsList.size());
		} else {
			setStartDate(dfOld.format(new Date()));
		}
		return "list";
	}

	public String sendSms() {
		if (StringUtils.isNotBlank(orderId)) {
			smsService.sendSms(orderContact, smsMsg, orderId);
			smsList = smsService.querySmsByOrderId(orderId);
		}

		return "viewOrderSms";
	}

	public String viewSms() {
		if (StringUtils.isNotBlank(orderId)) {
			smsList = smsService.querySmsByOrderId(orderId);
		}
		return "viewOrderSms";
	}

	public void prepare() throws Exception {
		clearErrorsAndMessages();
		// if (null == pager)
		// pager = new SimplePager();
		if (contactMenu == null) {
			contactMenu = menuService.get("Sms.Contacts");
		}
	}

}
